using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Fog : MonoBehaviour
{
    public static Fog Instance;

    public float alpha;
    private float speed = 3f;

    public List<CanvasGroup> canvasGroups;

    void Awake()
    {
        Instance = this;
        canvasGroups = new List<CanvasGroup>();
        GameObject.DontDestroyOnLoad(this);
    }


    // Start is called before the first frame update
    void Start()
    {
        CanvasGroup cg1 = transform.GetChild(0).GetComponent<CanvasGroup>();
        CanvasGroup cg2 = transform.GetChild(1).GetComponent<CanvasGroup>();
        CanvasGroup cg3 = transform.GetChild(2).GetComponent<CanvasGroup>();
        CanvasGroup cg4 = transform.GetChild(3).GetComponent<CanvasGroup>();
        CanvasGroup cg5 = transform.GetChild(4).GetComponent<CanvasGroup>();
        CanvasGroup cg6 = transform.GetChild(5).GetComponent<CanvasGroup>();
        canvasGroups.Add(cg1);
        canvasGroups.Add(cg2);
        canvasGroups.Add(cg3);
        canvasGroups.Add(cg4);
        canvasGroups.Add(cg5);
        canvasGroups.Add(cg6);
    }

    // Update is called once per frame
    void Update()
    {
        if (alpha > canvasGroups.First().alpha)
        {
            foreach (CanvasGroup cg in canvasGroups)
            {
                cg.alpha = Mathf.Clamp(cg.alpha + speed * Time.deltaTime, cg.alpha, alpha);  
            }
        }
        else if (alpha < canvasGroups.First().alpha)
        {
            foreach (CanvasGroup cg in canvasGroups)
            {
                cg.alpha = Mathf.Clamp(cg.alpha - speed * Time.deltaTime, alpha, cg.alpha);
            }
        }
    }

    public void changeFog(int grade)
    {
        switch (grade)
        {
            case 0:this.gameObject.SetActive(false); break;
            case 1: alpha = 0.5f; break;
            case 2: alpha = 0.8f; break;
        }
    }
    
}
